unit JATUIF_Driver_MultiScreen;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, contnrs, Mouse, Keyboard,
    JATUIF_Driver_Screen, JATUIF_Driver_Sound;

type

    { TMultiScreen }

    TMultiScreen = class(TObject)
      Public
        Constructor Create;
        Procedure Run;
        Procedure CreateScreen(Identifier:String);
        Function GetScreen(Identifier:String):TScreen;
        Procedure RemoveScreen(Identifier:String);
        Procedure ShowScreen(Identifier:String);
        Procedure Terminate;
        Procedure HardwareRelease;
        Procedure Eventize;
      Private
        ScreenObjects : TObjectList;
        ScreenIdentifiers : TStringList;
        ActiveScreen : Integer;
        Active : Boolean;
    end;

implementation

{ TMultiScreen }

constructor TMultiScreen.Create;
begin
     ScreenObjects:= TObjectList.create(true);
     ScreenIdentifiers:= TStringList.Create;
     ActiveScreen:= 0;
end;

procedure TMultiScreen.Run;
Var
   Screen : TScreen;

begin
     If ScreenObjects.Count > 0 then
        Active:= True;
     While Active do begin
           Screen:= ScreenObjects[ActiveScreen] as TScreen;
           Screen.Eventize;
     end;
end;

procedure TMultiScreen.CreateScreen(Identifier: String);
begin
     ScreenIdentifiers.Add(Identifier);
     ScreenObjects.Add(TScreen.Create);
end;

function TMultiScreen.GetScreen(Identifier: String): TScreen;
Var
   Index : Integer;

begin
     Index:= ScreenIdentifiers.IndexOf(Identifier);
     If Index = -1 then
        Exit;
     Result:= ScreenObjects[Index] as TScreen;
end;

procedure TMultiScreen.RemoveScreen(Identifier: String);
Var
   Index : Integer;

begin
     Index:= ScreenIdentifiers.IndexOf(Identifier);
     If Index = -1 then
        Exit;
     If Index = ActiveScreen then
        Exit;
     ScreenObjects.Delete(Index);
     ScreenIdentifiers.Delete(Index);
end;

procedure TMultiScreen.ShowScreen(Identifier: String);
Var
   Index : Integer;
   Active_Data : TDataArray;
   Active_Foreground : TColorArray;
   Active_Background : TColorArray;
   Screen : TScreen;

begin
     Index:= ScreenIdentifiers.IndexOf(Identifier);
     If Index = -1 then
        Exit;
     ActiveScreen:= Index;
     Screen:= ScreenObjects[ActiveScreen] as TScreen;
     Screen.SetActiveScreen(Active_Data, Active_Foreground, Active_Background);
end;

procedure TMultiScreen.Terminate;
begin
     Active:= False;
end;

procedure TMultiScreen.HardwareRelease;
begin
     DoneMouse;
     DoneKeyboard;
end;

procedure TMultiScreen.Eventize;
Var
   Screen : TScreen;

begin
     If ScreenObjects.Count > 0 then begin
        Screen:= ScreenObjects[ActiveScreen] as TScreen;
        Screen.Eventize;
     end;
end;

end.

